package com.javase.algorithm.datastructure.stringsandarrays;

import java.util.Arrays;

/**
 * 寻找数组的中心索引
 *
 * @author liyanhao
 * @create 2022-06-29 18:02
 * https://leetcode.cn/leetbook/read/array-and-string/yf47s/
 */
public class PivotIndex {
    public static void main(String[] args) {
        int[] arr = {1, 7, 3, 6, 5, 6};
        int i = new PivotIndex().pivotIndex(arr);
        System.out.println(i);
    }

    public int pivotIndex(int[] nums) {
        int total = Arrays.stream(nums).sum();
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            if (total == 2 * sum + nums[i]) {
                return i;
            }else{
                sum += nums[i];
            }
        }
        return -1;
    }
}
